<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
              <el-button type="primary" icon="el-icon-lx-add" @click="handleEdit" style="float:right">新增分类</el-button>
            </div>
            <el-table
                :data="tableData.records"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="categoryId" label="分类id"></el-table-column>
                <el-table-column prop="name" label="分类名称"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.status== 1?'success':'danger'"
                         >{{scope.row.status == 1?'可用':'禁用'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status==1">
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                        </div>
                        		  <el-button v-else class="yellow" type="text"  icon="el-icon-edit" @click="rec(scope.row.categoryId)">还原</el-button>

                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
            <el-pagination
                style="margin-top: 10px"
                background
                :current-page.sync="query.pageNum"
                :page-count="tableData.pages"
                :total="tableData.total"
                layout="total,prev,pager,next"
                @current-change="getData"
            ></el-pagination>
        </div>
        </div>

<!-- 编辑弹出框 -->
        <el-dialog title="新增分类" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="分类Id">
                    <el-input v-model="form.categoryId"></el-input>
                </el-form-item>
                <el-form-item label="分类名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import request from '@/utils/request';
import qee from "qf-export-excel"
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                search: '',
                pageNum: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            formLabelWidth: '120px',
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            request.get("http://localhost:8081/category/getCategories",{
               params:this.query
        }).then(res=>{
           this.$message.success('成功');
            this.tableData = res.data
        })
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let form = new FormData(); 
                    form.append("cId",row.categoryId)
                    request.post("http://localhost:8081/category/del",form).then(res=>{
                        this.$message.success('删除成功');
                        this.getData()
                    })
                    
                    
                })
                .catch(() => {});
        },
        rec(categoryId){
          // 还原
            this.$confirm('确定要还原吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let form = new FormData(); 
                    form.append("cId",categoryId)
                    request.post("http://localhost:8081/category/rec",form).then(res=>{
                        this.$message.success('还原成功');
                        this.getData()
                    })
                    
                    
                })
                .catch(() => {});      
        },      
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            const cIds = []
            this.delList.forEach(item=>{
                cIds.push(item.categoryId)
            })
            request.post("http://localhost:8081/category/mulDel",this.qs.stringify({ "cIds":cIds}, { arrayFormat: 'repeat' })).then(res=>{
                        this.$message.success('删除成功');
                        this.getData()
                    })
        },
        // 编辑操作
        handleEdit() {
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            let params = new FormData(); 
            for(const key in this.form){
                params.append(key,this.form[key]);
            }
             request.post("http://localhost:8081/category/add",params).then(res=>{
                        this.$message.success('修改成功');
                        this.editVisible = false;
                        this.getData()
                    })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        downExecl(){
            const titleList = [
			{ title:'用户id',key:'userid'},
			{ title:'用户名',key:'name'},
			{ title:'邮箱',key:'email'},
			{ title:'手机号',key:'tel'},
			{ title:'注册时间',key:'date'},
		]
		qee(titleList,this.tableData.records,'users')
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
    margin-left: 20px;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.yellow{
    color: rgb(136, 138, 18);
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
